import requests
import re
import execjs
import cchardet



url ="https://www.mps.gov.cn/n2254098/n4904352/"
info_url = 'https://www.mps.gov.cn/n2254098/n4904352/index_7574611_233.html'

headers = {
    'Referer': 'https://www.mps.gov.cn/n2254098/n4904352/',
    'Host': 'www.mps.gov.cn',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36'
}
session = requests.session()
session.headers = headers
res = session.get(url)

# 获取第一层得cookie
def get_first_cookie():
    pattern = re.compile(r'<script>document.cookie=(.*?);location.*?</script>')
    m = pattern.findall(res.text)[0]

    js_code = '''
        function get_cookie(m){
            return eval(m)
        }
    '''
    js = execjs.compile(js_code)
    cookie = js.call('get_cookie',m).split(';')[0].split("=")
    # 此处返回的是[]
    return cookie


def get_second_cookie(cookie):
    cookies = {
        cookie[0]:cookie[1]
    }
    session.cookies.update(cookies)

    ret = session.get(url)

    pattern = re.compile(r'.*?;go(.*?)</script>',re.S)
    m = pattern.findall(ret.text)[0]

    with open('js_final.js',encoding="utf-8") as f:
        js_final = f.read()

    js_final = js_final+ "go" + m

    js_ = execjs.compile(js_final)
    print(js_.call('get_cookie'))
    cookies = {
        '__jsl_clearance_s':js_.call('get_cookie')
    }

    session.cookies.update(cookies)
    return session


def get_content(session):
    r = session.get(info_url)
    encoding = cchardet.detect(r.content)['encoding']
    print(encoding)
    html = r.content.decode(encoding)
    print(html)


first_cookie = get_first_cookie()
session = get_second_cookie(first_cookie)
get_content(session)
